package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.common.TreeMenu;
import com.itheima.dto.PermissionsDto;
import com.itheima.entity.Permissions;
import com.itheima.service.PermissionsService;
import com.itheima.utils.TreeMenuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TangCheng
 * @date 2022/9/23 14:13
 * @description
 */
@RestController
@RequestMapping("/permissions")
public class PermissionsController {

    @Autowired
    private PermissionsService permissionsService;

    @GetMapping("/user")
    public R<List<Permissions>> getPermissions(Long empId) {

        List<Permissions> permissions = permissionsService.getPermissions(empId);

        return R.success(permissions);
    }

    @GetMapping("/allPermissions")
    public R<List<TreeMenu>> getAllPermissions() {
        List<TreeMenu> allMenus = TreeMenuUtils.getAllMenus();
        return R.success(allMenus);
    }

    @PostMapping
    public R<String> setPermission(@RequestBody PermissionsDto permissionsDto) {

        if (permissionsDto.getPermissions() == null || permissionsDto.getPermissions().size() == 0) {
            return R.error("权限设置不能为空！");
        }

        if (permissionsDto.getEmpId() == null) {
            return R.error("员工ID不能为空！");
        }

        permissionsService.setPermission(permissionsDto);

        return R.success("ok");
    }
}
